<?php
require_once '../utils/Database.php';
require_once '../utils/WeChatAuth.php';
require_once '../utils/Response.php';

$db = new Database();
$auth = new WeChatAuth();

$user = $auth->getCurrentUser();
if (!$user) {
    Response::authError('请先登录');
}

$method = $_SERVER['REQUEST_METHOD'];
$input = json_decode(file_get_contents('php://input'), true);

switch ($method) {
    case 'POST':
        // 活动申请
        $data = [
            'user_id' => $user['id'],
            'activity_name' => $input['activity_name'],
            'activity_type' => $input['activity_type'] ?? '',
            'activity_date' => $input['activity_date'],
            'start_time' => $input['start_time'],
            'end_time' => $input['end_time'],
            'participant_count' => $input['participant_count'],
            'organizer' => $input['organizer'],
            'contact_name' => $input['contact_name'],
            'contact_phone' => $input['contact_phone'],
            'activity_description' => $input['activity_description'] ?? '',
            'venue_requirements' => $input['venue_requirements'] ?? '',
            'equipment_needs' => $input['equipment_needs'] ?? '',
            'created_at' => date('Y-m-d H:i:s')
        ];
        
        $applicationId = $db->insert('activity_applications', $data);
        
        if ($applicationId) {
            Response::success(['application_id' => $applicationId], '活动申请提交成功');
        } else {
            Response::error('申请提交失败');
        }
        break;
        
    case 'GET':
        if (isset($_GET['action'])) {
            switch ($_GET['action']) {
                case 'list':
                    // 获取用户活动申请列表
                    $page = (int)($_GET['page'] ?? 1);
                    $limit = 10;
                    $offset = ($page - 1) * $limit;
                    
                    $applications = $db->fetchAll(
                        "SELECT id, activity_name, activity_type, activity_date, start_time, 
                         end_time, participant_count, organizer, status, created_at 
                         FROM activity_applications 
                         WHERE user_id = ? 
                         ORDER BY created_at DESC 
                         LIMIT ? OFFSET ?",
                        [$user['id'], $limit, $offset]
                    );
                    
                    Response::success($applications, '获取成功');
                    break;
                    
                case 'detail':
                    // 获取活动申请详情
                    $id = (int)$_GET['id'];
                    
                    $application = $db->fetchOne(
                        "SELECT * FROM activity_applications WHERE id = ? AND user_id = ?",
                        [$id, $user['id']]
                    );
                    
                    if ($application) {
                        Response::success($application, '获取成功');
                    } else {
                        Response::notFound('申请记录不存在');
                    }
                    break;
                    
                default:
                    Response::error('无效的操作');
            }
        } else {
            Response::error('缺少action参数');
        }
        break;
        
    case 'PUT':
        // 取消活动申请
        if (isset($_GET['action']) && $_GET['action'] == 'cancel') {
            $id = (int)$input['id'];
            
            $result = $db->update(
                'activity_applications',
                ['status' => 3, 'updated_at' => date('Y-m-d H:i:s')],
                'id = ? AND user_id = ? AND status = 0',
                [$id, $user['id']]
            );
            
            if ($result) {
                Response::success(null, '活动申请已取消');
            } else {
                Response::error('取消失败，申请可能已处理或不存在');
            }
        } else {
            Response::error('无效的操作');
        }
        break;
        
    default:
        Response::error('不支持的请求方法');
}
?>